package org.linlinjava.litemall.admin.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.util.Children;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/admin/region")
@Validated
public class AdminRegionController {
    private final Log logger = LogFactory.getLog(AdminRegionController.class);

    @Autowired
    private LitemallRegionService regionService;

    @GetMapping("/clist")
    public Object clist(@NotNull Integer id) {
        List<LitemallRegion> regionList = regionService.queryByPid(id);
        return ResponseUtil.okList(regionList);
    }

    @GetMapping("/list")
    public Object list() {
//        List<RegionVo> regionVoList = new ArrayList<>();

        List<LitemallRegion> provinceList = regionService.queryByPid(0);
//        for (LitemallRegion province : provinceList) {
//            RegionVo provinceVO = new RegionVo();
//            provinceVO.setId(province.getId());
//            provinceVO.setName(province.getName());
//            provinceVO.setCode(province.getCode());
//            provinceVO.setType(province.getType());
//
//            List<LitemallRegion> cityList = regionService.queryByPid(province.getId());
//            List<RegionVo> cityVOList = new ArrayList<>();
//            for (LitemallRegion city : cityList) {
//                RegionVo cityVO = new RegionVo();
//                cityVO.setId(city.getId());
//                cityVO.setName(city.getName());
//                cityVO.setCode(city.getCode());
//                cityVO.setType(city.getType());
//
//                List<LitemallRegion> areaList = regionService.queryByPid(city.getId());
//                List<RegionVo> areaVOList = new ArrayList<>();
//                for (LitemallRegion area : areaList) {
//                    RegionVo areaVO = new RegionVo();
//                    areaVO.setId(area.getId());
//                    areaVO.setName(area.getName());
//                    areaVO.setCode(area.getCode());
//                    areaVO.setType(area.getType());
//                    areaVOList.add(areaVO);
//                }
//
//                cityVO.setChildren(areaVOList);
//                cityVOList.add(cityVO);
//            }
//            provinceVO.setChildren(cityVOList);
//            regionVoList.add(provinceVO);
//        }

        return ResponseUtil.okList(provinceList);
    }


//    @GetMapping("/json")
    public Object json() {
        List<Children> cityList = new LinkedList<>();
        cityList = changeForm(regionService.queryByPid(0));
        for (Children city : cityList) {
            city.children = changeForm(regionService.queryByParentCode(Integer.valueOf(city.value)));
            if (city.children.size() > 0) {
                for (Children country : city.children) {
                     country.children = changeForm(regionService.queryByParentCode(Integer.valueOf(country.value)));
                }
            }
        }
        return cityList;
    }

    @PostMapping("/json")
    public Object json(@RequestBody String msg) {
        List<Children> childrenList = JSONObject.parseArray(msg, Children.class);
//        List<Children> childrenList = JacksonUtil.parseObject(msg, "areajson", List.class);
        for (int p = 0; p < childrenList.size(); p ++) {
            LitemallRegion province = new LitemallRegion();
            Children pChildren = childrenList.get(p);
            province.setCode(Integer.valueOf(pChildren.value));
            province.setName(pChildren.label);
            province.setPid(0);
            province.setType((byte)1);
            regionService.add(province);
            if (pChildren.getChildren() != null) {
                for (int c = 0; c < pChildren.getChildren().size(); c++) {
                    Children cChildren = pChildren.getChildren().get(c);
                    LitemallRegion city = new LitemallRegion();
                    city.setCode(Integer.valueOf(cChildren.value));
                    city.setName(cChildren.label);
                    city.setPid(province.getId());
                    city.setType((byte) 2);
                    regionService.add(city);
                    if (cChildren.getChildren() != null) {
                        for (int c2 = 0; c2 < cChildren.getChildren().size(); c2++) {
                            Children children = cChildren.getChildren().get(c2);
                            LitemallRegion country = new LitemallRegion();
                            country.setCode(Integer.valueOf(children.value));
                            country.setName(children.label);
                            country.setPid(city.getId());
                            country.setType((byte) 3);
                            regionService.add(country);
                        }
                    }
                }
            }
        }
        return "ok";
    }


    private List<Children> changeForm(List<LitemallRegion> regionList) {
        List<Children> childrenList = new LinkedList<>();
        for (LitemallRegion region : regionList) {
            childrenList.add(new Children(region));
        }
        return childrenList;
    }
}
